package com.redoor.app.stake.workflow.service.impl;

import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoor.app.stake.basicdata.model.BussinessTypeEnum;
import com.redoor.app.stake.basicdata.service.BaseSystemOperationLogService;
import com.redoor.app.stake.common.utils.UUIDTool;
import com.redoor.app.stake.workflow.commons.WorkFlowStatus;
import com.redoor.app.stake.workflow.model.BaseWorkorder;
import com.redoor.app.stake.workflow.model.controllerModel.WorkOrderCriteria;
import com.redoor.app.stake.workflow.service.BaseWorkorderOperationLogService;
import com.redoor.app.stake.workflow.service.BaseWorkorderService;
import com.redoor.app.stake.workflow.service.MaintenaceChargeWorkOrderService;
import com.redoornetwork.framework.core.exception.ServiceException;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MaintenaceChargeWorkOrderServiceImpl implements MaintenaceChargeWorkOrderService {

    private static final Logger logger = LoggerFactory.getLogger(MaintenaceChargeWorkOrderServiceImpl.class);
    @Autowired
    private BaseWorkorderService baseWorkorderService;

    @Autowired
    private BaseWorkorderOperationLogService operationLogService;

    @Autowired
    private BaseSystemOperationLogService baseSystemOperationLogService;

    @Autowired
    private BaseUserService userService;

    @Override
    public int create(BaseWorkorder mainApplyWorkOrder) {
        if (null == mainApplyWorkOrder
                || StringUtils.isBlank(mainApplyWorkOrder.getUuid())){
            /*抛出参数为空异常*/
            logger.info("MaintenaceChargeWorkOrderServiceImpl_create_paramError");
            throw new ServiceException(WorkFlowStatus.PARAM_IS_EMPTY);
        }
        BaseWorkorder workorder = new BaseWorkorder();
        /*生成uuid由公共的insert方法转移到这个地方,因为保存工单操作日志的时候需要这个uuid*/
        workorder.setUuid(UUIDTool.getUUID());
        /*保存工单类型*/
        workorder.setTargettype(WorkFlowStatus.WORKORDER_TARGET_TYPE_MAINTENANCECHARGE.getCode());
        /*保存目标uuid*/
        workorder.setTargetuuid(mainApplyWorkOrder.getTargetuuid());
        /*保存工单类型名称*/
        workorder.setContent(WorkFlowStatus.WORKORDER_TARGET_TYPE_MAINTENANCECHARGE.getMessage());
        /*保存目标组织机构代码*/
        workorder.setCreateorgcode(mainApplyWorkOrder.getCreateorgcode());
        /*保存创建人uuid*/
        workorder.setCreatoruuid(userService.getShiroUserUuid());
        /*添加操作日志*/
        if (!operationLogService.maChargeCreateLog(workorder)){
            logger.error("MaintenaceChargeWorkOrderServiceImpl_create_addMaChargeCreateLogError");
        }
        /*添加页面操作日志*/
        baseSystemOperationLogService.addSystemOperationLog(BussinessTypeEnum.WEB_OPERATOR_TYPE.WORK_ORDER, BussinessTypeEnum.TAG_TYPE.ADD,workorder.getUuid(),"",true);

        return baseWorkorderService.insert(workorder);
    }

    @Override
    public int accept(WorkOrderCriteria workOrderCriteria) {
        BaseWorkorder baseWorkorder = baseWorkorderService.findById(workOrderCriteria.getUuid());
        baseWorkorder.setWorkstatus(Integer.parseInt(WorkFlowStatus.WORKORDER_STATUS_FINISH.getCode()));
        /*写入审核人uuid*/
        baseWorkorder.setReviewruuid(userService.getShiroUserUuid());
        /*写入审核意见*/
        baseWorkorder.setRemark(workOrderCriteria.getReviewSuggestion());
        /*添加操作日志*/
        if (!operationLogService.maChargeAcceptLog(baseWorkorder)){
            logger.error("MaintenaceChargeWorkOrderServiceImpl_accept_addMaChargeAccetpError");
        }
        /*添加页面操作日志*/
        baseSystemOperationLogService.addSystemOperationLog(BussinessTypeEnum.WEB_OPERATOR_TYPE.WORK_ORDER, BussinessTypeEnum.TAG_TYPE.UPDATE,baseWorkorder.getUuid(),"",true);

        return baseWorkorderService.update(baseWorkorder);
    }

    @Override
    public int reject(WorkOrderCriteria workOrderCriteria) {
        BaseWorkorder baseWorkorder = baseWorkorderService.findById(workOrderCriteria.getUuid());
        baseWorkorder.setWorkstatus(Integer.parseInt(WorkFlowStatus.WORKORDER_STATUS_REJECT.getCode()));
        /*写入审核人uuid*/
        baseWorkorder.setReviewruuid(userService.getShiroUserUuid());
        /*写入审核意见*/
        baseWorkorder.setRemark(workOrderCriteria.getReviewSuggestion());
        /*天骄操作日志*/
        if (!operationLogService.maChargeRejectLog(baseWorkorder)){
            logger.error("MaintenaceChargeWorkOrderServiceImpl_reject_addMaChargeRejectError");
        }
        /*添加页面操作日志*/
        baseSystemOperationLogService.addSystemOperationLog(BussinessTypeEnum.WEB_OPERATOR_TYPE.WORK_ORDER, BussinessTypeEnum.TAG_TYPE.UPDATE,baseWorkorder.getUuid(),"",true);

        return baseWorkorderService.update(baseWorkorder);
    }

    @Override
    public int active(BaseWorkorder baseWorkorder) {
        baseWorkorder.setWorkstatus(Integer.parseInt(WorkFlowStatus.WORKORDER_STATUS_APPLYING.getCode()));
        /*添加操作日志*/
        if (!operationLogService.maChargeActiveLog(baseWorkorder)){
            logger.error("MaintenaceChargeWorkOrderServiceImpl_active_addMaChargeActiveError");
        }
        /*添加页面操作日志*/
        baseSystemOperationLogService.addSystemOperationLog(BussinessTypeEnum.WEB_OPERATOR_TYPE.WORK_ORDER, BussinessTypeEnum.TAG_TYPE.UPDATE,baseWorkorder.getUuid(),"",true);

        return baseWorkorderService.update(baseWorkorder);
    }
}
